import GameManager from "../../script/GameManager";
// import Core from "../../Script/frame/core/Core";
import Event from "../../script/event/EventManager";
import EventType from "../../script/event/EventType";
// import MathUtil from "../../Script/frame/utils/MathUtil";
import GameData from "../../script/GameData";
import Hero from "../../script/ctrl/Hero";
import GameUI from "../../script/GameUI";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Joystick extends cc.Component {
  static ins: Joystick = null;
  @property(cc.Node)
  circle_big: cc.Node = null;

  @property(cc.Node)
  circle_small: cc.Node = null;

  //是否禁用摇杆
  public _isDisable: boolean = false;

  // 小圆移动的半径长度
  public radius: number = 0;
  //是否按下
  public isDown: boolean = false;

  public player: Hero = null;

  //速度
  speed: number = 225; //角色移动速度

  vector: cc.Vec2 = cc.v2(0, 0); //移动向量

  onLoad() {
    Joystick.ins = this;
    // 获取小圆能移动的最大半径长度
    this.radius = this.circle_big.width / 2 - this.circle_small.width / 3;
    // let node = cc.Canvas.instance.node;
    let node = this.node.parent;
    node.on(cc.Node.EventType.TOUCH_START, this.onMouseStart, this);
    node.on(cc.Node.EventType.TOUCH_MOVE, this.onMouseMove, this);
    node.on(cc.Node.EventType.TOUCH_END, this.onMouseUp, this);
    node.on(cc.Node.EventType.TOUCH_CANCEL, this.onMouseUp, this);
    node.active = true;
  }

  start() {}

  init(role: Hero) {
    this.player = role;
  }

  private isFirst: boolean = false;
  private isFirst2: boolean = false;

  update(dt) {
    // if (this.isDown && this.player && !this.player.stopMove && !this.player._isDie) {
    //     // cc.log(dt.toFixed(4));
    //     let v = this.player.getComponent(cc.RigidBody).linearVelocity;
    //     // let x = this.player.node.x + this.vector.x * this.speed * dt;
    //     // let y = this.player.node.y + this.vector.y * this.speed * dt;//每帧获取角色的坐标加上移动向量
    //     let x = v.x * this.speed * dt;
    //     let y = v.y * this.speed * dt;//每帧获取角色的坐标加上移动向量
    //     //让角色的坐标每帧为自身的坐标加上移动的向量
    //     this.player.node.x = x;
    //     this.player.node.y = y;
    //     this.player.getComponent(cc.RigidBody).linearVelocity.x = x
    //     this.player.getComponent(cc.RigidBody).linearVelocity.y = y
    //     let rotation = 0;
    //     //求出角色的旋转角度
    //     // if (this.vector.y < 0) {//当摇杆在原点下方时
    //     //     //角度是负的
    //     //     rotation = (-this.vector.angle(cc.v2(1, 0))) / Math.PI * 180;//根据向量先求出弧度，再求出角度
    //     // } else {//如果摇杆在原点上方时
    //     //     //角度是正的
    //     //     rotation = (this.vector.angle(cc.v2(1, 0))) / Math.PI * 180;//根据向量先求出弧度，再求出角度
    //     // }
    //     // console.log(rotation);
    //     // this.player.getDir(rotation);
    // }
  }

  onMouseStart(event: cc.Event.EventTouch) {
    // GameManager.instance.startClick(null);
    if (!GameData.isJoystickMove) return;
    GameUI.instance.clear();

    if (!this.isDisable) {
      this.node.active = true;
      this.isDown = true;
      let pos = this.node.parent.convertToNodeSpaceAR(event.getLocation());
      this.node.x = pos.x;
      this.node.y = pos.y;
      Event.getInstance().emit(EventType.JOYSTICK_STATE, true);
    }
  }

  onMouseMove(event: cc.Event.EventTouch) {
    if (!this.isDisable && this.isDown) {
      GameUI.instance.clear();
      let pos_0 = this.node.convertToNodeSpaceAR(event.getLocation()); //将一个点转换到节点 (局部) 空间坐标系，这个坐标系以锚点为原点。
      // let pos1 = this.node.parent.convertToNodeSpaceAR(event.getLocation());
      // const distanceX = pos1.x - this.node.x;
      // const distanceY = pos1.y - this.node.y;

      //pos_0.mag()是这个触点的长度
      if (pos_0.mag() < this.radius) {
        //如果触点长度小于我们规定好的最大半径
        this.circle_small.x = pos_0.x; //摇杆的坐标为触点坐标
        this.circle_small.y = pos_0.y;
      } else {
        //如果不
        let pos = pos_0.normalizeSelf(); //将触点归一化
        let x = pos.x * this.radius; //归一化的触点坐标 × 最大半径
        let y = pos.y * this.radius;

        this.circle_small.x = x; //给摇杆坐标赋值
        this.circle_small.y = y;
      }

      let dir = this.circle_small.position.normalizeSelf(); //dir为摇杆坐标的归一化
      // this.vector.x = dir.x * this.speed;//给移动向量赋值
      // this.vector.y = dir.y * this.speed;//移动向量为方向 × 速度
      this.vector.x = dir.x;
      this.vector.y = dir.y;
      // const angle = Math.atan2(distanceX, distanceY) * 180 / Math.PI;

      let rotation = 0;
      //求出角色的旋转角度
      if (this.vector.y < 0) {
        //当摇杆在原点下方时
        //角度是负的
        rotation = (-this.vector.angle(cc.v2(1, 0)) / Math.PI) * 180; //根据向量先求出弧度，再求出角度
      } else {
        //如果摇杆在原点上方时
        //角度是正的
        rotation = (this.vector.angle(cc.v2(1, 0)) / Math.PI) * 180; //根据向量先求出弧度，再求出角度
      }
      if (GameUI.instance) {
        GameData.jiaoDu = rotation;
        GameUI.instance.directMove(rotation, this.vector);
      }
    }
  }

  onMouseUp(event: cc.Event.EventTouch) {
    if (this.isDown) {
      this.stop();
      GameUI.instance.clear();
      GameUI.instance.stopRocker();
      if (!this.isFirst) {
        this.isFirst = true;
      }
    }
  }

  public get isDisable(): boolean {
    return this._isDisable;
  }

  public set isDisable(value: boolean) {
    this._isDisable = value;
    if (value) {
      this.stop();
    }
  }

  /**停止 */
  public stop() {
    this.isDown = false;
    this.circle_small.setPosition(0, 0);
    this.node.active = false;
    this.vector.x = 0;
    this.vector.y = 0;
  }
}
